#include "cPopupMenu.h"
#include "cHwnd.h"


cPopupMenu::cPopupMenu(cHwnd * _parent)
{
    //ctor
    parent=_parent;
    menu=CreatePopupMenu();
    MENUINFO infoMenu={0};
    infoMenu.cbSize=sizeof(infoMenu);
    infoMenu.fMask=MIM_STYLE;
    infoMenu.dwStyle=MNS_NOTIFYBYPOS;
    SetMenuInfo(menu,&infoMenu);
    idmenu=0;
}

cPopupMenu::~cPopupMenu()
{
    //dtor
}

void cPopupMenu::AddItem(string label,evenementPopup fonctionRetour)
{
    //AppendMenu( menu, MF_STRING, idmenu, label.c_str() );
    char * texte=new char[label.size()+1];
    sprintf(texte,"%s",label.c_str());

    MenuItem * infoitem=new MenuItem;
    infoitem->libelle=label;
    infoitem->fonction=fonctionRetour;

    MENUITEMINFO popmenuinfo={0};
    popmenuinfo.cbSize=sizeof(popmenuinfo);
    popmenuinfo.fMask=MIIM_DATA|MIIM_TYPE;
    popmenuinfo.fType=MFT_STRING;
    popmenuinfo.cch=sizeof(texte);
    popmenuinfo.dwTypeData=texte;
    popmenuinfo.dwItemData=(DWORD)infoitem;
    InsertMenuItem(menu,idmenu,true,&popmenuinfo);


    idmenu++;
}

void cPopupMenu::AddItem(string label, cPopupMenu * sousMenu)
{
    char * texte=new char[label.size()+1];
    sprintf(texte,"%s",label.c_str());


    MENUITEMINFO popmenuinfo={0};
    popmenuinfo.cbSize=sizeof(popmenuinfo);
    popmenuinfo.fMask=MIIM_TYPE|MIIM_SUBMENU;
    popmenuinfo.fType=MFT_STRING;
    popmenuinfo.hSubMenu=sousMenu->GetMenu();
    popmenuinfo.cch=sizeof(texte);
    popmenuinfo.dwTypeData=texte;
    InsertMenuItem(menu,idmenu,true,&popmenuinfo);

    idmenu++;

    //AppendMenu( menu, MF_STRING|MF_POPUP, (LPARAM)sousMenu->GetMenu(), label.c_str() );
}

void cPopupMenu::AddLine()
{
    MENUITEMINFO popmenuinfo={0};
    popmenuinfo.cbSize=sizeof(popmenuinfo);
    popmenuinfo.fMask=MIIM_TYPE;
    popmenuinfo.fType=MFT_SEPARATOR;
    InsertMenuItem(menu,idmenu,true,&popmenuinfo);

    idmenu++;
}

void cPopupMenu::AddTitre(string titre)
{
    char * texte=new char[titre.size()+1];
    sprintf(texte,"%s",titre.c_str());


    MENUITEMINFO popmenuinfo={0};
    popmenuinfo.cbSize=sizeof(popmenuinfo);
    popmenuinfo.fMask=MIIM_TYPE|MIIM_STATE;
    popmenuinfo.fType=MFT_STRING;
    popmenuinfo.fState=MFS_DISABLED;
    popmenuinfo.cch=sizeof(texte);
    popmenuinfo.dwTypeData=texte;
    InsertMenuItem(menu,idmenu,true,&popmenuinfo);


    idmenu++;

}

void cPopupMenu::Popup()
{
    POINT   pt;
    GetCursorPos( &pt );
    TrackPopupMenuEx( menu
                    , 0
                    , pt.x, pt.y
                    , parent->getHwnd()
                    , NULL );

    //DestroyMenu( menu );
}
